<template>
  <div class="home">
    <el-row :gutter="24" class="overview">
      <el-col :xs="24" :sm="8" :md="8" v-for="(stat, index) in stats" :key="index" class="stat-col">
        <el-card shadow="hover" class="stat-card">
          <template #header>
            <div class="card-header">
              <span>{{ stat.title }}</span>
              <el-icon :size="20" :color="stat.iconColor"><component :is="stat.icon" /></el-icon>
            </div>
          </template>
          <div class="stat-number">{{ stat.value }}</div>
        </el-card>
      </el-col>
    </el-row>

    <div class="recent-plans">
      <div class="section-header">
        <h2 class="section-title">最近计划</h2>
        <router-link to="/plan-list" class="view-all">
          <el-link type="primary" :underline="false" class="view-all-link">
            查看全部 <el-icon><ArrowRight /></el-icon>
          </el-link>
        </router-link>
      </div>
      
      <el-row :gutter="24" v-if="recentPlans.length > 0">
        <el-col :xs="24" :sm="12" :md="12" v-for="plan in recentPlans" :key="plan.id" class="plan-col">
          <router-link :to="`/check-in/${plan.id}`" class="plan-link">
            <el-card shadow="hover" class="plan-card">
              <div class="plan-info">
                <h3 class="plan-title">{{ plan.title }}</h3>
                <p class="plan-description">{{ plan.description }}</p>
              </div>
              <ProgressBar
                :current="plan.completedDays"
                :total="plan.totalDays"
                text="完成天数"
              />
            </el-card>
          </router-link>
        </el-col>
      </el-row>
      
      <el-empty v-else description="还没有创建任何计划" class="empty-state">
        <router-link to="/plan-create">
          <el-button type="primary">立即创建</el-button>
        </router-link>
      </el-empty>
    </div>
  </div>
</template>

<script>
import ProgressBar from '@/components/ProgressBar.vue'
import { ArrowRight, Plus, Calendar, Bell, Medal } from '@element-plus/icons-vue'

export default {
  name: 'HomeView',
  components: {
    ProgressBar,
    ArrowRight,
    Plus,
    Calendar,
    Bell,
    Medal
  },
  data() {
    return {
      activePlans: 0,
      todayPending: 0,
      totalCheckIns: 0,
      recentPlans: []
    }
  },
  computed: {
    stats() {
      return [
        {
          title: '进行中的计划',
          value: this.activePlans,
          icon: 'Calendar',
          iconColor: '#409EFF'
        },
        {
          title: '今日待打卡',
          value: this.todayPending,
          icon: 'Bell',
          iconColor: '#E6A23C'
        },
        {
          title: '累计打卡',
          value: this.totalCheckIns,
          icon: 'Medal',
          iconColor: '#67C23A'
        }
      ]
    }
  },
  created() {
    // TODO: 从Vuex获取数据
    // 模拟数据
    this.activePlans = 3
    this.todayPending = 2
    this.totalCheckIns = 45
    this.recentPlans = [
      {
        id: 1,
        title: '每日阅读',
        description: '每天阅读30分钟',
        completedDays: 15,
        totalDays: 30
      },
      {
        id: 2,
        title: '运动健身',
        description: '每天运动1小时',
        completedDays: 8,
        totalDays: 30
      }
    ]
  }
}
</script>

<style scoped>
.home {
  max-width: 1200px;
  margin: 0 auto;
  padding: 24px;
  background-color: #f5f7fa;
  min-height: calc(100vh - 60px);
}

/* 移除了 nav-header 相关样式 */

.overview {
  margin-bottom: 40px;
}

.stat-col {
  margin-bottom: 20px;
}

.stat-card {
  height: 100%;
  transition: all 0.3s;
  border-radius: 8px;
  overflow: hidden;
}

.stat-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px;
  background-color: #f9fafc;
}

.card-header span {
  font-size: 16px;
  font-weight: 500;
  color: #606266;
}

.stat-number {
  font-size: 42px;
  font-weight: bold;
  color: var(--el-color-primary);
  text-align: center;
  padding: 24px 0;
}

.section-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.section-title {
  margin: 0;
  color: #303133;
  font-size: 22px;
  font-weight: 600;
}

.view-all-link {
  font-size: 15px;
  display: flex;
  align-items: center;
}

.view-all-link .el-icon {
  margin-left: 4px;
  transition: transform 0.3s;
}

.view-all-link:hover .el-icon {
  transform: translateX(4px);
}

.recent-plans {
  margin-top: 30px;
  background-color: white;
  padding: 24px;
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
}

.plan-col {
  margin-bottom: 24px;
}

.plan-link {
  text-decoration: none;  /* 移除下划线 */
  color: inherit;         /* 使用继承的颜色 */
  display: block;         /* 确保链接占据整个区域 */
}

.plan-card {
  height: 100%;
  transition: all 0.3s;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
}

.plan-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.plan-info {
  margin-bottom: 16px;
}

.plan-title {
  margin: 0 0 12px;
  color: #303133;
  font-size: 18px;
  font-weight: 600;
}

.plan-description {
  margin: 0 0 16px;
  color: #606266;
  font-size: 14px;
  line-height: 1.6;
}

.plan-actions {
  display: flex;
  gap: 12px;
  margin-top: 20px;
  justify-content: space-between;
}

.action-link {
  width: 45%;  /* 设置为45%，而不是50%，以便在中间留有一些间距 */
  text-align: center;
}

.action-button {
  width: 100%;  /* 按钮占据链接的全部宽度 */
  padding: 10px 0;
}

.empty-state {
  padding: 40px 0;
}

@media (max-width: 768px) {
  .home {
    padding: 16px;
  }
  
  .page-title {
    font-size: 24px;
  }
  
  .stat-number {
    font-size: 36px;
    font-weight: bold;
    color: #4A90E2;  /* 修改为理性蓝 */
    text-align: center;
  }
  
  .section-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
  }
  
  .section-header h2 {
    margin: 0;
    color: #2D3436;  /* 修改为深灰 */
  }
  
  .recent-plans {
    padding: 16px;
  }
}
</style>